<?php // $Id: block_moodlelibreserve_forms.php,v 1.0.0.0 2010-07-14 16:32 pthayer Exp $

require_once('../../config.php');
require_once('../../lib/accesslib.php');
require_once('lib.php');

global $CFG, $USER, $COURSE;

@$get_reserve = stripslashes($_GET['get_reserve']);
@$level = stripslashes($_GET['level']);
@$data = stripslashes($_GET['data']);
@$selection = stripslashes($_GET['selection']);
@$sel_id = stripslashes($_GET['sel_id']);
@$call_num = stripslashes($_GET['call_num']);
@$cid = stripslashes($_GET['cid']);
@$comment = stripslashes($_GET['comment']);
@$id = stripslashes($_GET['id']);
@$delete_request = stripslashes($_GET['delete_request']);

$level?'':$level=1;
$back = $level-1;
$next = $level+1;
$json_array = json_decode($data);
$form = '<form name="myform">';
$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);

switch($level) {

	case 0:
		$sql = "SELECT * FROM {$CFG->prefix}libres WHERE course={$COURSE->id}";
		if($rs = get_records_sql($sql)){
            $form .= '<div style="font-weight:bold;">Your pending requests:</div>';
            foreach($rs as $op){
                @$form .= '<div style="background-color:#ddd;padding:2px;margin-bottom:3px;"><a onmousedown="get_reserve('.$op->id.', \''.$CFG->wwwroot.'\'); return false;">'.$op->title.'</a></div>';
            }
        }
        if(has_capability('block/moodlelibreserve:makerequest', $context)){
		    $form .= '<div><a href="instructor.php">Make a new request</a></div>';
        }
		break;

	case 1:
		$sql = "SELECT * FROM {$CFG->prefix}request_types ORDER BY id asc";
		if($rs = get_records_sql($sql)){
			$form .= '<table style="border:0px;">';
			$cat = '';
            foreach($rs as $op){
				$cat_style = '';
				$print_cat = '';
				if($op->cat !== $cat){
					$cat = $op->cat;
					$print_cat = $op->cat;
					$cat_style = 'border-top:2px solid black;margin-top:10px;';
				}
                @$json_array->req_type == $op->id?$checked = 'checked="yes"':$checked = '';
                $form .= '<tr style="'.$cat_style.'"><td><span style="font-weight:bold;">'.$print_cat.' </span></td><td><input type="radio" name="req_type" value="'.$op->id.'" '.$checked.' onmouseup="fdata.user_id='.$USER->id.';fdata.course='.$cid.';fdata.req_type=this.value; return false;" />'.$op->description.'</td></tr>';
            }
			$form .= '<tr><td></td><td><br /><a href="#" onmouseup="init_libres_form(2, \''.$CFG->wwwroot.'\', '.$cid.'); return false;">Next</a></td></tr>';
			$form .= '</table>';
        }else{
            $form .= '<div style="color:red;">Request types have not been set. Please ask an administrator to set request types in the Library Reserve block settings.</div>';
        }
		break;

	case 2:
		$form .= '<p>Title:<br />';
		@$form .= '<input type="text" name="title" value="'.$json_array->title.'" /></p>';
        $form .= '<p>Author/Artist/Director:<br />';
		@$form .= '<input type="text" name="author" value="'.$json_array->author.'" /></p>';
		$form .= '<p>Year/Edition:<br />';
		@$form .= '<input type="text" name="year" value="'.$json_array->year.'" /></p>';
		$form .= '<p>Publisher:<br />';
		@$form .= '<input type="text" name="publisher" value="'.$json_array->publisher.'" /></p>';
		$form .= '<div style="float:left;margin-right:5px;"><a onmousedown="init_libres_form('.$back.', \''.$CFG->wwwroot.'\', '.$cid.'); return false;">Back</a></div>';
		$form .= '<div style="float:left;"><a onmousedown="fdata.title=document.myform.title.value; fdata.author=document.myform.author.value; fdata.publisher=document.myform.publisher.value; fdata.year = document.myform.year.value; init_libres_form('.$next.', \''.$CFG->wwwroot.'\', '.$cid.'); return false;">Next</a></div><br style="height:0px;clear:both;" />';
		break;

    case 3:
        if($json_array->req_type == 3){
			$level = 4;
		}else{
			if($json_array->req_type == 1){$style1 = 'display:none;';}elseif($json_array->req_type == 2){$style2 = 'display:none;';}
			@$form .= '<div style="'.$style2.'">Library call number:<br />';
			@$form .= '<input type="text" name="call_num" value="'.$json_array->call_num.'" /></div>';
			$form .= '<div style="'.$style1.'">ISBN number:<br />';
			@$form .= '<input type="text" name="isbn_num" value="'.$json_array->isbn_num.'" /></div>';
		    $form .= '<div style="float:left;margin-right:5px;"><a onmousedown="init_libres_form('.$back.', \''.$CFG->wwwroot.'\', '.$cid.'); return false;">Back</a></div>';
		    $form .= '<div style="float:left;"><a onmousedown="fdata.call_num=document.myform.call_num.value; fdata.isbn_num=document.myform.isbn_num.value; init_libres_form('.$next.', \''.$CFG->wwwroot.'\', '.$cid.'); return false;">Next</a></div><br style="height:0px;clear:both;" />';
		    break;
        }

    case 4:
        $form .= '<div><a onmousedown="init_libres_form('.$next.', \''.$CFG->wwwroot.'\', '.$cid.'); return false;">Finish</a></div>';
        $form .= '<div style="float:left;margin-right:5px;"><a onmousedown="init_libres_form('.$back.', \''.$CFG->wwwroot.'\', '.$cid.'); return false;">Back</a></div>';
        break;

	case 5:
		$json_array->timecreated = time();
		foreach($json_array as $key => $value){
			$c[] = $key;
			$v[] = addslashes($value);
			$json_array->$key = addslashes($value);
		}
		$cols = implode(', ', $c);
		$vals = implode(', ', $v);
//		return(var_dump($json_array));
		if(!$json_array->course || !$json_array->user_id || $json_array->course == '0'){
			$form .= "<div style=\"color:red;font-weight:bold;\">Error: The request data is malformed. Please go back and review the information you submitted. This could be caused by an ampersand (&) in a field. Please use 'and' instead.</div>";
		}else{
			if(@$json_array->id){
				$insert = update_record('libres', $json_array);
			}else{
				$insert = insert_record('libres', $json_array);
			}
		
			if(!$insert){
				$form .= "<div style=\"color:red;font-weight:bold;\">There was an error finalizing the request. Please review the provided information.</div>";
				$form .= '<div><a onmousedown="init_libres_form('.$back.', \''.$CFG->wwwroot.'\', '.$cid.'); return false;">Back</a></div>';

			}else{
				$form .= "Your request has been submitted.";
				$form .= '<div><a href="'.$CFG->wwwroot.'/blocks/moodlelibreserve/instructor.php?cid='.$cid.'">OK</a></div>';
			}
		}
		break;

	case 6:
		$sql = "SELECT * FROM {$CFG->prefix}libres WHERE id=$get_reserve limit 1";
		$rs = get_recordset_sql($sql);
		echo json_encode($rs->fields);
		break;

	case 7:
		$sql = "UPDATE {$CFG->prefix}libres SET processing=$selection, call_num='$call_num' WHERE id=$sel_id";
		$rs = execute_sql($sql, false);
		$rs = get_recordset('libres', 'id', $sel_id);
		$rs = rs_fetch_next_record($rs);
		if($rs){
			echo '<p>The changes have been saved.</p>';
			echo '<input type="radio" name="req_status" id="req_status" value="0" ';
			if($rs->processing == 0){echo 'checked="true"';}
			echo " />is not being processed.<br />";
			echo '<input type="radio" name="req_status" id="req_status" value="1" ';	
			if($rs->processing == 1){echo 'checked="true"';}
			echo '/>is being processed.<br />';
			echo '<input type="radio" name="req_status" id="req_status" value="2" ';
			if($rs->processing == 2){echo 'checked="true"';}
			echo '/>has been processed.<br />';
		}else{
			echo "Sorry, an error occurred. Please refresh the page and try again.";
		}
		exit();
		break;

	case 8:
		echo libres_comments($comment, $id, '');
		return;
		break;

	case 9:
		$sql = "DELETE FROM {$CFG->prefix}libres WHERE id=$delete_request limit 1";
		$rs = execute_sql($sql);
		break;

	default:
		$form .= "Some quirky error has occurred. How on Earth did you do that?";
		$form .= '<div style="margin-right:5px;"><a onmousedown="init_libres_form('.$back.', \''.$CFG->wwwroot.'\', '.$cid.'); return false;">Back</a></div>';
}

if($level != 6){
	$form .= '</form>';
	echo $form;
}
?>
